package com.qixin.modules.rzInvoice.service.impl;

import com.qixin.modules.rzInvoice.entity.EaRzZzszyfp;
import com.qixin.modules.rzInvoice.entity.EaRzZzszyfpItems;
import com.qixin.modules.rzInvoice.mapper.EaRzZzszyfpItemsMapper;
import com.qixin.modules.rzInvoice.mapper.EaRzZzszyfpMapper;
import com.qixin.modules.rzInvoice.service.IEaRzZzszyfpService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 增值税专用发票
 * @Author: qixin-boot
 * @Date:   2021-07-10
 * @Version: V1.0
 */
@Service
public class EaRzZzszyfpServiceImpl extends ServiceImpl<EaRzZzszyfpMapper, EaRzZzszyfp> implements IEaRzZzszyfpService {

	@Autowired
	private EaRzZzszyfpMapper eaRzZzszyfpMapper;
	@Autowired
	private EaRzZzszyfpItemsMapper eaRzZzszyfpItemsMapper;
	
	@Override
	@Transactional
	public void saveMain(EaRzZzszyfp eaRzZzszyfp, List<EaRzZzszyfpItems> eaRzZzszyfpItemsList) {
		eaRzZzszyfpMapper.insert(eaRzZzszyfp);
		if(eaRzZzszyfpItemsList!=null && eaRzZzszyfpItemsList.size()>0) {
			for(EaRzZzszyfpItems entity:eaRzZzszyfpItemsList) {
				//外键设置
				entity.setRzZzszyfpId(eaRzZzszyfp.getId());
				eaRzZzszyfpItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaRzZzszyfp eaRzZzszyfp,List<EaRzZzszyfpItems> eaRzZzszyfpItemsList) {
		eaRzZzszyfpMapper.updateById(eaRzZzszyfp);
		
		//1.先删除子表数据
		eaRzZzszyfpItemsMapper.deleteByMainId(eaRzZzszyfp.getId());
		
		//2.子表数据重新插入
		if(eaRzZzszyfpItemsList!=null && eaRzZzszyfpItemsList.size()>0) {
			for(EaRzZzszyfpItems entity:eaRzZzszyfpItemsList) {
				//外键设置
				entity.setRzZzszyfpId(eaRzZzszyfp.getId());
				eaRzZzszyfpItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaRzZzszyfpItemsMapper.deleteByMainId(id);
		eaRzZzszyfpMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaRzZzszyfpItemsMapper.deleteByMainId(id.toString());
			eaRzZzszyfpMapper.deleteById(id);
		}
	}
	
}
